package com.scheduler;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.utils.HttpUtil;
import com.common.utils.Result;
import com.entity.*;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

@Component
@Configurable
@EnableScheduling
public class SharesScheduler {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SharesCodeService sharesCodeService;

    @Autowired
    private HistoryDataService historyDataService;

    @Autowired
    private SharesZdfTrend001Service sharesZdfTrend001Service;

    @Autowired
    private SharesZdfTrend002Service sharesZdfTrend002Service;

    @Autowired
    private SharesZdfTrend003Service sharesZdfTrend003Service;

    @Autowired
    private SharesZdfTrend300Service sharesZdfTrend300Service;

    @Autowired
    private SharesZdfTrend600Service sharesZdfTrend600Service;

    @Autowired
    private SharesZdfTrend601Service sharesZdfTrend601Service;

    @Autowired
    private SharesZdfTrend603Service sharesZdfTrend603Service;

    @Autowired
    private SharesZdfTrend605Service sharesZdfTrend605Service;

    @Autowired
    private SharesZdfTrend688Service sharesZdfTrend688Service;

    @Autowired
    private SharesZdfTrend000Service sharesZdfTrend000Service;

    @Autowired
    private SharesDataProbRedService sharesDataProbRedService;
    /**
     * 统计每天的概率  新
     */
    @Scheduled(cron = "0 41 22 5 * *")
    public void getProbEveryNew(){
        Wrapper<SharesDataProbRed> wr=new EntityWrapper<>();
//        wr.eq("time_str","2021-03-29");
        String time ="2021-04-30";
        wr.eq("time_str",time);
        wr.gt("id",110983);
        List<SharesDataProbRed> sharesDataProbs = sharesDataProbRedService.selectList(wr);
        if(CollectionUtils.isEmpty(sharesDataProbs)){
            return;
        }

        for (SharesDataProbRed sharesDataProb : sharesDataProbs) {
            System.out.println(sharesDataProb.getCode());
            sharesDataProbRedService.getPorbByCodeAndTrend(sharesDataProb);
            dealData(sharesDataProb);
            sharesDataProbRedService.updateById(sharesDataProb);
        }

        System.out.println("start");
       // sharesDataProbRedService.updateBatchById(sharesDataProbs);
    }

    /**
     * 处理数据，进行保存
     * @param sharesDataProb
     */
    private void dealData(SharesDataProbRed sharesDataProb) {
        //单只3-7天
        Integer probSevenAdd = sharesDataProb.getProbSevenAdd();//0
        Integer probSevenSub = sharesDataProb.getProbSevenSub();//1
        Integer probSixAdd = sharesDataProb.getProbSixAdd();//2
        Integer probSixSub = sharesDataProb.getProbSixSub();//3
        Integer probFiveAdd = sharesDataProb.getProbFiveAdd();//4
        Integer probFiveSub = sharesDataProb.getProbFiveSub();//5
        Integer probFourAdd = sharesDataProb.getProbFourAdd();//6
        Integer probFourSub = sharesDataProb.getProbFourSub();//7
        Integer probThreeAdd = sharesDataProb.getProbThreeAdd();//8
        Integer probThreeSub = sharesDataProb.getProbThreeSub();//9

        //板块5-7天
        Integer probSevenPlateAdd = sharesDataProb.getProbSevenPlateAdd();//10
        Integer probSevenPlateSub = sharesDataProb.getProbSevenPlateSub();//11
        Integer probSixPlateAdd = sharesDataProb.getProbSixPlateAdd();//12
        Integer probSixPlateSub = sharesDataProb.getProbSixPlateSub();//13
        Integer probFivePlateAdd = sharesDataProb.getProbFivePlateAdd();//14
        Integer probFivePlateSub = sharesDataProb.getProbFivePlateSub();//15


        //全部5-7天
        Integer probSevenAllAdd = sharesDataProb.getProbSevenAllAdd();//16
        Integer probSevenAllSub = sharesDataProb.getProbSevenAllSub();//17
        Integer probSixAllAdd = sharesDataProb.getProbSixAllAdd();//18
        Integer probSixAllSub = sharesDataProb.getProbSixAllSub();//19
        Integer probFiveAllAdd = sharesDataProb.getProbFiveAllAdd();//20
        Integer probFiveAllSub = sharesDataProb.getProbFiveAllSub();//21

        //后天单支7天 Green
        Integer probSevenAddGreen = sharesDataProb.getProbSevenGreenAdd();//22
        Integer probSevenSubGreen = sharesDataProb.getProbSevenGreenSub();//23
        Integer probSixAddGreen = sharesDataProb.getProbSixGreenAdd();//24
        Integer probSixSubGreen = sharesDataProb.getProbSixGreenSub();//25
        Integer probFiveAddGreen = sharesDataProb.getProbFiveGreenAdd();//26
        Integer probFiveSubGreen = sharesDataProb.getProbFiveGreenSub();//27
        Integer probFourAddGreen = sharesDataProb.getProbFourGreenAdd();//28
        Integer probFourSubGreen = sharesDataProb.getProbFourGreenSub();//29
        Integer probThreeAddGreen = sharesDataProb.getProbThreeGreenAdd();//30
        Integer probThreeSubGreen = sharesDataProb.getProbThreeGreenSub();//31

        //板块5-7天
        Integer probSevenPlateAddGreen = sharesDataProb.getProbSevenPlateGreenAdd();//32
        Integer probSevenPlateSubGreen = sharesDataProb.getProbSevenPlateGreenSub();//33
        Integer probSixPlateAddGreen = sharesDataProb.getProbSixPlateGreenAdd();//34
        Integer probSixPlateSubGreen = sharesDataProb.getProbSixPlateGreenSub();//35
        Integer probFivePlateAddGreen = sharesDataProb.getProbFivePlateGreenAdd();//36
        Integer probFivePlateSubGreen = sharesDataProb.getProbFivePlateGreenSub();//37

        //全部5-7天
        Integer probSevenAllAddGreen = sharesDataProb.getProbSevenAllGreenAdd();//38
        Integer probSevenAllSubGreen = sharesDataProb.getProbSevenAllGreenSub();//39
        Integer probSixAllAddGreen = sharesDataProb.getProbSixAllGreenAdd();//40
        Integer probSixAllSubGreen = sharesDataProb.getProbSixAllGreenSub();//41
        Integer probFiveAllAddGreen = sharesDataProb.getProbFiveAllGreenAdd();//42
        Integer probFiveAllSubGreen = sharesDataProb.getProbFiveAllGreenSub();//43

        //zdf
        Integer zdfProbFivePlateAdd = sharesDataProb.getZdfProbFivePlateAdd();//44
        Integer zdfProbFivePlateSub = sharesDataProb.getZdfProbFivePlateSub();//45
        Integer zdfProbFourPlateAdd = sharesDataProb.getZdfProbFourPlateAdd();//46
        Integer zdfProbFourPlateSub = sharesDataProb.getZdfProbFourPlateSub();//47
        Integer zdfProbThreePlateAdd = sharesDataProb.getZdfProbThreePlateAdd();//48
        Integer zdfProbThreePlateSub = sharesDataProb.getZdfProbThreePlateSub();//49


        List<Integer>  list=new ArrayList<>();
        //单支7天
        list.add(probSevenAdd);
        list.add(probSevenSub);
        list.add(probSixAdd);
        list.add(probSixSub);
        list.add(probFiveAdd);
        list.add(probFiveSub);
        list.add(probFourAdd);
        list.add(probFourSub);
        list.add(probThreeAdd);
        list.add(probThreeSub);

        //板块567
        list.add(probSevenPlateAdd);
        list.add(probSevenPlateSub);
        list.add(probSixPlateAdd);
        list.add(probSixPlateSub);
        list.add(probFivePlateAdd);
        list.add(probFivePlateSub);

        //全部567
        list.add(probSevenAllAdd);
        list.add(probSevenAllSub);
        list.add(probSixAllAdd);
        list.add(probSixAllSub);
        list.add(probFiveAllAdd);
        list.add(probFiveAllSub);


        //后天单只7天
        list.add(probSevenAddGreen);
        list.add(probSevenSubGreen);
        list.add(probSixAddGreen);
        list.add(probSixSubGreen);
        list.add(probFiveAddGreen);
        list.add(probFiveSubGreen);
        list.add(probFourAddGreen);
        list.add(probFourSubGreen);
        list.add(probThreeAddGreen);
        list.add(probThreeSubGreen);

        //后天板块567
        list.add(probSevenPlateAddGreen);
        list.add(probSevenPlateSubGreen);
        list.add(probSixPlateAddGreen);
        list.add(probSixPlateSubGreen);
        list.add(probFivePlateAddGreen);
        list.add(probFivePlateSubGreen);

        //后天全部567
        list.add(probSevenAllAddGreen);
        list.add(probSevenAllSubGreen);
        list.add(probSixAllAddGreen);
        list.add(probSixAllSubGreen);
        list.add(probFiveAllAddGreen);
        list.add(probFiveAllSubGreen);

        //zdf
        list.add(zdfProbFivePlateAdd);
        list.add(zdfProbFivePlateSub);
        list.add(zdfProbFourPlateAdd);
        list.add(zdfProbFourPlateSub);
        list.add(zdfProbThreePlateAdd);
        list.add(zdfProbThreePlateSub);

        String s = JSONArray.toJSONString(list);

        sharesDataProb.setDetail(s);

    }

    /**
     * 获取每支股票的走势
     */
    @Scheduled(cron = "0 11 20 5 * *")
    public void getSharesTrend(){
        Wrapper<SharesCode> wr=new EntityWrapper<>();
        wr.isNull("status");
        List<SharesCode> sharesCodes = sharesCodeService.selectList(wr);
        for (SharesCode sharesCode : sharesCodes) {
            sharesCode.setStatus(3);
            String code = sharesCode.getCode();
            System.out.println(code);
            getSharesData(code);
        }
//        getSharesData("000002");
    };

    /**
     * 获取最近一段时间的数据
     */
    private void getSharesData(String code){
        String url="https://api.doctorxiong.club/v1/stock/kline/day?code="+code+"&startDate=2021-04-01&endDate=2021-05-05&type=1&token=Ho8gFD2ZH0";
        String s = HttpUtil.sendGet(url, null);
        JSONObject json = JSON.parseObject(s);
        String data = json.getString("data");
        List<String> strings = JSONArray.parseArray(data, String.class);

        List<SharesDataProbRed> sharesDataProbReds=new ArrayList<>();
        if(!CollectionUtils.isEmpty(strings)){
        for (String string : strings) {
            List<String> shareData = JSONArray.parseArray(string, String.class);
//            System.out.println(shareData.get(0));
//            System.out.println(shareData.get(1));
//            System.out.println(shareData.get(2));


            SharesDataProbRed sharesDataProbRed = new SharesDataProbRed();
            sharesDataProbRed.setCode(code);
            sharesDataProbRed.setTimeStr(shareData.get(0));//日期
            sharesDataProbRed.setStartPrice(new BigDecimal(shareData.get(1)));//开盘价
            sharesDataProbRed.setEndPrice(new BigDecimal(shareData.get(2)));//收盘价
            sharesDataProbReds.add(sharesDataProbRed);
        }
        }else{

            System.out.println("====="+code);
        }

        if(!CollectionUtils.isEmpty(sharesDataProbReds)&&sharesDataProbReds.size()>9){

                int size = sharesDataProbReds.size();
                SharesDataProbRed sharesDataProbRed1 = sharesDataProbReds.get(size - 1);
                SharesDataProbRed sharesDataProbRed2 = sharesDataProbReds.get(size - 2);
                SharesDataProbRed sharesDataProbRed3 = sharesDataProbReds.get(size - 3);
                SharesDataProbRed sharesDataProbRed4 = sharesDataProbReds.get(size - 4);
                SharesDataProbRed sharesDataProbRed5 = sharesDataProbReds.get(size - 5);
                SharesDataProbRed sharesDataProbRed6 = sharesDataProbReds.get(size - 6);
                SharesDataProbRed sharesDataProbRed7 = sharesDataProbReds.get(size - 7);
                SharesDataProbRed sharesDataProbRed8 = sharesDataProbReds.get(size - 8);

                //1
                sharesDataProbRed1.setZdf(
                        getzdfstartandend(
                                sharesDataProbRed1.getEndPrice().doubleValue(),
                                sharesDataProbRed2.getEndPrice().doubleValue()
                        ));

                //2
                sharesDataProbRed2.setZdf(
                        getzdfstartandend(
                                sharesDataProbRed2.getEndPrice().doubleValue(),
                                sharesDataProbRed3.getEndPrice().doubleValue()
                        ));

                //3
                sharesDataProbRed3.setZdf(
                        getzdfstartandend(
                                sharesDataProbRed3.getEndPrice().doubleValue(),
                                sharesDataProbRed4.getEndPrice().doubleValue()
                        ));

                //4
                sharesDataProbRed4.setZdf(
                        getzdfstartandend(
                                sharesDataProbRed4.getEndPrice().doubleValue(),
                                sharesDataProbRed5.getEndPrice().doubleValue()
                        ));

                //5
                sharesDataProbRed5.setZdf(
                        getzdfstartandend(
                                sharesDataProbRed5.getEndPrice().doubleValue(),
                                sharesDataProbRed6.getEndPrice().doubleValue()
                        ));

                //6
                sharesDataProbRed6.setZdf(
                        getzdfstartandend(
                                sharesDataProbRed6.getEndPrice().doubleValue(),
                                sharesDataProbRed7.getEndPrice().doubleValue()
                        ));

                //7
                sharesDataProbRed7.setZdf(
                        getzdfstartandend(
                                sharesDataProbRed7.getEndPrice().doubleValue(),
                                sharesDataProbRed8.getEndPrice().doubleValue()
                        ));

                sharesDataProbRed1.setZdfTrend(
                        sharesDataProbRed7.getZdf()
                        +sharesDataProbRed6.getZdf()
                        +sharesDataProbRed5.getZdf()
                        +sharesDataProbRed4.getZdf()
                        +sharesDataProbRed3.getZdf()
                        +sharesDataProbRed2.getZdf()
                        +sharesDataProbRed1.getZdf()
                );

            sharesDataProbRedService.insert(sharesDataProbRed1);

        }

    }

    /**
     * 计算涨跌
     */
    public String getzdfstartandend(double start,double end){

//        double start=sd.getStartPrice().doubleValue();
//        double end=sd.getEndPrice().doubleValue();
        double zdf = (end - start) / start;

        if (zdf >= 0) {
            int zdfint = (int) ((zdf * 100) + 0.5);
            if (zdfint >= 10) {
                zdfint = 9;
            }
            return  "+"+zdfint;
        } else {
            int zdfint = (int) ((zdf * 100) - 0.5);
            if (zdfint == 0) {
                return  "-"+zdfint;
            } else {
                if (zdfint <= -10) {
                    zdfint = -9;
                }
                return  ""+zdfint;
            }
        }
    }


    /**
     * 计算7天的走势
     */
//    private void


    /**
     * 计算板块走势
     */
    @Scheduled(cron = "0 11 17 5 * *")
    public void getTrend(){

        List<Integer> list=new ArrayList<>();
        list.add(16);
        list.add(14);
        list.add(12);
        list.add(10);
        list.add(8);
        list.add(6);
        Map<String,Integer> trendMapAll=new HashMap<>();

        //000
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend000> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend000> sharesZdfTrend000s = sharesZdfTrend000Service.selectList(wr);
            System.out.println("000"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend000 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("000"+s);
                if(null==integer){
                    trendMap.put("000"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("000"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);


            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }


        //001
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend001> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend001> sharesZdfTrend000s = sharesZdfTrend001Service.selectList(wr);
            System.out.println("001"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend001 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("001"+s);
                if(null==integer){
                    trendMap.put("001"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("001"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }


        //002
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend002> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend002> sharesZdfTrend000s = sharesZdfTrend002Service.selectList(wr);
            System.out.println("002"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend002 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("002"+s);
                if(null==integer){
                    trendMap.put("002"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("002"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }


        //003
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend003> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend003> sharesZdfTrend000s = sharesZdfTrend003Service.selectList(wr);
            System.out.println("003"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend003 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("003"+s);
                if(null==integer){
                    trendMap.put("003"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("003"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }


        //300
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend300> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend300> sharesZdfTrend000s = sharesZdfTrend300Service.selectList(wr);
            System.out.println("300"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend300 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("300"+s);
                if(null==integer){
                    trendMap.put("300"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("300"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }

        //600
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend600> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend600> sharesZdfTrend000s = sharesZdfTrend600Service.selectList(wr);
            System.out.println("600"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend600 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("600"+s);
                if(null==integer){
                    trendMap.put("600"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("600"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }

        //601
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend601> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend601> sharesZdfTrend000s = sharesZdfTrend601Service.selectList(wr);
            System.out.println("601"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend601 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("601"+s);
                if(null==integer){
                    trendMap.put("601"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("601"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }

        //603
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend603> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend603> sharesZdfTrend000s = sharesZdfTrend603Service.selectList(wr);
            System.out.println("603"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend603 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("603"+s);
                if(null==integer){
                    trendMap.put("603"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("603"+s,integer);
                }


                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }

        //605
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend605> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend605> sharesZdfTrend000s = sharesZdfTrend605Service.selectList(wr);
            System.out.println("605"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend605 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("605"+s);
                if(null==integer){
                    trendMap.put("605"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("605"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }

        //688
        for (Integer len : list) {
            Map<String,Integer> trendMap=new HashMap<>();
            Wrapper<SharesZdfTrend688> wr=new EntityWrapper<>();
            wr.eq("len",len);
            List<SharesZdfTrend688> sharesZdfTrend000s = sharesZdfTrend688Service.selectList(wr);
            System.out.println("688"+"len="+len+"===size="+sharesZdfTrend000s.size());
            for (SharesZdfTrend688 sharesZdfTrend000 : sharesZdfTrend000s) {
                String code = sharesZdfTrend000.getCode();
                String trend = sharesZdfTrend000.getTrend();
                Integer num = sharesZdfTrend000.getNum();

                String s = trend.replaceAll("\\d+", "");
                Integer integer = trendMap.get("688"+s);
                if(null==integer){
                    trendMap.put("688"+s,1);
                }else{
                    integer=integer+num;
                    trendMap.put("688"+s,integer);
                }

                allTrendMap(trendMapAll,num,s);
            }
            //保存map trendMap
            savaMaptrend(trendMap);
        }
        savaMaptrend(trendMapAll);
        

    }

    private void allTrendMap(Map<String, Integer> trendMapAll, Integer num, String s) {

        Integer integerAll = trendMapAll.get("all"+s);
        if(null==integerAll){
            trendMapAll.put("all"+s,1);
        }else{
            integerAll=integerAll+num;
            trendMapAll.put("all"+s,integerAll);
        }
    }

    /**
     * 计算单只股票  +++--- 的走势概率
     */
    @Scheduled(cron = "0 32 16 5 * *")
    public void getSingleShare(){
            Wrapper<SharesCode> wr=new EntityWrapper<>();
            wr.isNull("status");
            List<SharesCode> sharesCodes = sharesCodeService.selectList(wr);
            for (SharesCode sharesCode : sharesCodes) {
                sharesCode.setStatus(3);
                sharesCodeService.updateById(sharesCode);
                String code = sharesCode.getCode();
                System.out.println(code);
                Wrapper<HistoryData> hiswr=new EntityWrapper<>();
                hiswr.eq("code",code);
                hiswr.orderBy("d",true);
                System.out.println(code);
                List<HistoryData> historyData = historyDataService.selectList(hiswr);
                if(!CollectionUtils.isEmpty(historyData)){

                    for (int i = 0; i < historyData.size(); i++) {
                        if (i < 1) {
                            continue;
                        }
                        double cBeffur = historyData.get(i - 1).getC().doubleValue();
                        double cAfter = historyData.get(i).getC().doubleValue();
                        double zdf = (cAfter - cBeffur) / cBeffur;

                        if (zdf >= 0) {
                            int zdfint = (int) ((zdf * 100) + 0.5);
                            if (zdfint >= 10) {
                                zdfint = 9;
                            }
                            historyData.get(i).setZdf("+" + zdfint);
                        } else {
                            int zdfint = (int) ((zdf * 100) - 0.5);
                            if (zdfint == 0) {
                                historyData.get(i).setZdf("-" + zdfint);
                            } else {
                                if (zdfint <= -10) {
                                    zdfint = -9;
                                }
                                historyData.get(i).setZdf("" + zdfint);
                            }
                        }
                    }


                    Map<String,Integer> mapStr=new HashMap<>();
                    for(int i=0;i<historyData.size();i++){
                        if(i<9){
                            continue;
                        }
                        HistoryData h0 = historyData.get(i);
                        HistoryData h1 = historyData.get(i - 1);
                        HistoryData h2 = historyData.get(i - 2);
                        HistoryData h3 = historyData.get(i - 3);
                        HistoryData h4 = historyData.get(i - 4);
                        HistoryData h5 = historyData.get(i - 5);
                        HistoryData h6 = historyData.get(i - 6);
                        HistoryData h7 = historyData.get(i - 7);

                        String zdf7 = h7.getZdf();
                        String zdf6 = h6.getZdf();
                        String zdf5 = h5.getZdf();
                        String zdf4 = h4.getZdf();
                        String zdf3 = h3.getZdf();
                        String zdf2 = h2.getZdf();
                        String zdf1 = h1.getZdf();
                        String zdf0 = h0.getZdf();

                        //近8天的涨跌幅  +-+-+-+-
                        String zdf7str=zdf7+zdf6+zdf5+zdf4+zdf3+zdf2+zdf1+zdf0;
                        //近7天的涨幅
                        String zdf6str=zdf6+zdf5+zdf4+zdf3+zdf2+zdf1+zdf0;
                        //近6天的涨幅
                        String zdf5str=zdf5+zdf4+zdf3+zdf2+zdf1+zdf0;
                        //近5天的涨幅
                        String zdf4str=zdf4+zdf3+zdf2+zdf1+zdf0;
                        //近4天的涨幅
                        String zdf3str=zdf3+zdf2+zdf1+zdf0;

                        prossMap(mapStr,code+zdf7str.replaceAll("\\d+", ""));
                        prossMap(mapStr,code+zdf6str.replaceAll("\\d+", ""));
                        prossMap(mapStr,code+zdf5str.replaceAll("\\d+", ""));
                        prossMap(mapStr,code+zdf4str.replaceAll("\\d+", ""));
                        prossMap(mapStr,code+zdf3str.replaceAll("\\d+", ""));


                    }

//                redis保存
                    if(!CollectionUtils.isEmpty(mapStr)){
                        int size = mapStr.keySet().size();
                        System.out.println("size="+size);
                        redisSaveCodeMap(mapStr);
                    }

                }
            }
        

    }
    /**
     *
     * @param mapStr
     */
    private void redisSaveCodeMap(Map<String, Integer> mapStr) {
        if(!CollectionUtils.isEmpty(mapStr)){
            redisTemplate.opsForValue().multiSet(mapStr);
        }
    }

    /**
     * @param mapStr
     * @param zdf7str
     */
    private void prossMap(Map<String, Integer> mapStr, String zdf7str) {
        Integer integer = mapStr.get(zdf7str);
        if(null==integer){
            mapStr.put(zdf7str,1);
        }else{
            integer= integer+1;
            mapStr.put(zdf7str,integer);
        }

    }

    private void savaMaptrend(Map<String, Integer> trendMap) {
        Set<String> strings = trendMap.keySet();
        Iterator<String> iterator = strings.iterator();

        Map<String,Integer>  map50=new HashMap<>();
        Map<String,Integer>  map100=new HashMap<>();
        Map<String,Integer>  map150=new HashMap<>();
        int i=0;
        while(iterator.hasNext()){
            i++;
            String next = iterator.next();
            if(i<50*10000){
                Integer integer = trendMap.get(next);
                map50.put(next,integer);
            }else if(50*10000<=i&&i<100*10000){
                Integer integer = trendMap.get(next);
                map100.put(next,integer);
            }else{
                Integer integer = trendMap.get(next);
                map150.put(next,integer);
            }
        }

        saveMapInredis(map50);
        saveMapInredis(map100);
        saveMapInredis(map150);
    }

    private void saveMapInredis(Map<String, Integer> map) {
        if(!CollectionUtils.isEmpty(map)){
            redisTemplate.opsForValue().multiSet(map);
        }
    }

    public static void main(String[] args) {


//        String code="000001";
//        String url="https://api.doctorxiong.club/v1/stock/kline/day?code="+code+"&startDate=2021-04-01&endDate=2021-05-05&type=1";
//        String s = HttpUtil.sendGet(url, null);
//        System.out.println(s);
//        String data="[[\"2021-04-01\",\"22.080\",\"21.780\",\"22.110\",\"21.500\",\"544449.000\"],[\"2021-04-02\",\"21.700\",\"21.500\",\"21.730\",\"21.360\",\"581084.000\"],[\"2021-04-06\",\"21.550\",\"21.680\",\"22.090\",\"21.510\",\"401103.000\"],[\"2021-04-07\",\"21.880\",\"21.640\",\"21.930\",\"21.310\",\"517605.000\"],[\"2021-04-08\",\"21.460\",\"21.560\",\"21.730\",\"21.330\",\"383030.000\"],[\"2021-04-09\",\"21.710\",\"21.300\",\"21.720\",\"21.080\",\"398960.000\"],[\"2021-04-12\",\"21.510\",\"20.700\",\"21.510\",\"20.630\",\"594743.000\"],[\"2021-04-13\",\"20.720\",\"20.780\",\"20.880\",\"20.590\",\"499904.000\"],[\"2021-04-14\",\"20.790\",\"20.670\",\"20.940\",\"20.420\",\"537438.000\"],[\"2021-04-15\",\"20.760\",\"20.360\",\"20.770\",\"20.080\",\"735865.000\"],[\"2021-04-16\",\"20.360\",\"20.260\",\"20.430\",\"19.810\",\"729998.000\"],[\"2021-04-19\",\"20.030\",\"21.150\",\"21.240\",\"19.910\",\"1112283.000\"],[\"2021-04-20\",\"21.080\",\"21.690\",\"21.950\",\"20.920\",\"834773.000\"],[\"2021-04-21\",\"22.300\",\"23.010\",\"23.190\",\"22.290\",\"1597100.000\"],[\"2021-04-22\",\"23.190\",\"22.980\",\"23.230\",\"22.780\",\"829694.000\"],[\"2021-04-23\",\"23.320\",\"23.290\",\"23.650\",\"23.070\",\"823230.000\"],[\"2021-04-26\",\"23.870\",\"22.940\",\"24.230\",\"22.900\",\"872418.000\"],[\"2021-04-27\",\"22.950\",\"22.940\",\"23.190\",\"22.860\",\"470431.000\"],[\"2021-04-28\",\"23.290\",\"23.350\",\"23.450\",\"22.780\",\"593838.000\"],[\"2021-04-29\",\"23.340\",\"23.590\",\"23.710\",\"23.110\",\"614837.000\"],[\"2021-04-30\",\"23.350\",\"23.290\",\"23.490\",\"23.010\",\"561981.000\"]]";
//        List<String> strings = JSON.parseArray(data, String.class);
//        for (String string : strings) {
//            System.out.println(string);
//            List<String> sharesData = JSON.parseArray(string, String.class);
//            System.out.println(sharesData.get(0));//日期
//            System.out.println(sharesData.get(1));//开盘价
//            System.out.println(sharesData.get(2));//收盘价
//        }


        String s="{\"code\":200,\"message\":\"操作成功\",\"data\":[[\"2021-04-01\",\"22.080\",\"21.780\",\"22.110\",\"21.500\",\"544449.000\"],[\"2021-04-02\",\"21.700\",\"21.500\",\"21.730\",\"21.360\",\"581084.000\"],[\"2021-04-06\",\"21.550\",\"21.680\",\"22.090\",\"21.510\",\"401103.000\"],[\"2021-04-07\",\"21.880\",\"21.640\",\"21.930\",\"21.310\",\"517605.000\"],[\"2021-04-08\",\"21.460\",\"21.560\",\"21.730\",\"21.330\",\"383030.000\"],[\"2021-04-09\",\"21.710\",\"21.300\",\"21.720\",\"21.080\",\"398960.000\"],[\"2021-04-12\",\"21.510\",\"20.700\",\"21.510\",\"20.630\",\"594743.000\"],[\"2021-04-13\",\"20.720\",\"20.780\",\"20.880\",\"20.590\",\"499904.000\"],[\"2021-04-14\",\"20.790\",\"20.670\",\"20.940\",\"20.420\",\"537438.000\"],[\"2021-04-15\",\"20.760\",\"20.360\",\"20.770\",\"20.080\",\"735865.000\"],[\"2021-04-16\",\"20.360\",\"20.260\",\"20.430\",\"19.810\",\"729998.000\"],[\"2021-04-19\",\"20.030\",\"21.150\",\"21.240\",\"19.910\",\"1112283.000\"],[\"2021-04-20\",\"21.080\",\"21.690\",\"21.950\",\"20.920\",\"834773.000\"],[\"2021-04-21\",\"22.300\",\"23.010\",\"23.190\",\"22.290\",\"1597100.000\"],[\"2021-04-22\",\"23.190\",\"22.980\",\"23.230\",\"22.780\",\"829694.000\"],[\"2021-04-23\",\"23.320\",\"23.290\",\"23.650\",\"23.070\",\"823230.000\"],[\"2021-04-26\",\"23.870\",\"22.940\",\"24.230\",\"22.900\",\"872418.000\"],[\"2021-04-27\",\"22.950\",\"22.940\",\"23.190\",\"22.860\",\"470431.000\"],[\"2021-04-28\",\"23.290\",\"23.350\",\"23.450\",\"22.780\",\"593838.000\"],[\"2021-04-29\",\"23.340\",\"23.590\",\"23.710\",\"23.110\",\"614837.000\"],[\"2021-04-30\",\"23.350\",\"23.290\",\"23.490\",\"23.010\",\"561981.000\"]],\"meta\":\"sz000001\"}";
        JSONObject json = JSON.parseObject(s);
        String data = json.getString("data");
        System.out.println(data);
    }


}
